package cn.neusoft.service.impl;

import cn.neusoft.domain.Commodity;
import cn.neusoft.domain.Customer;
import cn.neusoft.mapper.CommodityClassMapper;
import cn.neusoft.query.BaseQuery;
import cn.neusoft.service.CommodityClassService;
import cn.neusoft.vo.PageBean;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.neusoft.domain.CommodityClass;
import java.util.List;

@Service
public class CommodityClassServiceImpl implements CommodityClassService {
    @Autowired
    CommodityClassMapper commodityClassMapper;

    @Override
    @Transactional
    public void add(CommodityClass c){commodityClassMapper.add(c);};
    @Override
    @Transactional
    public void delete(Integer id){
        commodityClassMapper.delete(id);
    };
    @Override
    @Transactional
    public void update(CommodityClass c){
        commodityClassMapper.update(c);
    };
    @Override
    @Transactional
    public List<CommodityClass> getAll(){return commodityClassMapper.getAll();
    };

    @Override
    @Transactional
    public CommodityClass getById(Integer id){
        return commodityClassMapper.getById(id);
    };

    @Override
    public List<CommodityClass> getByName(String name) {
        return commodityClassMapper.getByName(name);
    }

    @Override
    public List<CommodityClass> getCommodityClassesByPage(int page, int pageSize) {
        int start = (page - 1) * pageSize;
        return commodityClassMapper.getCommodityClassesByPage(start, pageSize);
    }

    @Override
    public int getTotalCount() {
        return commodityClassMapper.getTotalCount();
    }

    @Override
    public PageBean<CommodityClass> findByPage(BaseQuery query){ //构建结果对象
        PageBean<CommodityClass> pageBean = new PageBean<>();
        //开启分页
        PageHelper.startPage(query.getPageNow(),query.getPageSize());
        //分页查询得到当页的集合
        List<CommodityClass> commodityClassList = commodityClassMapper.findByPage(query);
        //创建一个分页的实体PageInfo
        PageInfo<CommodityClass> pageInfo = new PageInfo<>(commodityClassList);

        pageBean.setTotalRows(pageInfo.getTotal());
        pageBean.setTotalPages(pageInfo.getPages());
        pageBean.setRowData(pageInfo.getList());

        return pageBean;};

    @Override
    public void deleteByFirstClass(String first_class) {
        commodityClassMapper.deleteByFirstClass(first_class);
    }

    @Override
    public void deleteByFirstClassSecond(String first_class){
        commodityClassMapper.deleteByFirstClassSecond(first_class);
    };
}
